package com.example.xgakj.newterminal.ui.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;

import com.example.xgakj.mylibrary.mvp.utils.CommonUtil;
import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.models.LoginModel;
import com.socks.library.KLog;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //申请所有权限
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1){
            requestAllPerms();
        }else {
            countTime();
        }



    }

    private void countTime() {
        //倒计时2秒后检查是否自动登录
        Observable.just(3, 2, 1).zipWith(Observable.interval(0, 1, TimeUnit.SECONDS), new BiFunction<Integer, Long, String>() {
            @Override
            public String apply(@NonNull Integer integer, @NonNull Long aLong) throws Exception {
                return String.valueOf(integer);
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addSubscription(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        KLog.d(s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        KLog.d("onError");
                    }

                    @Override
                    public void onComplete() {
                        KLog.d(Thread.currentThread().getName());
                        checkLogin();
                    }
                });
    }

    /**
     * 根据SP判断是否直接登录还是进入登录界面
     */
    private void checkLogin() {
        String userName = SharedPreferencesUtil.getString(mContex, Parameters.USER_NAME, "");
        String pass_word = SharedPreferencesUtil.getString(mContex, Parameters.PASS_WORD, "");
        //如果SP存有账号密码，就调用登录方法
        if (!CommonUtil.isNull(userName)&&!CommonUtil.isNull(pass_word)){
            LoginModel loginModel=new LoginModel();
            loginModel.login(userName,pass_word,this);
        }else {
            Intent intent=new Intent(this,LoginActivity.class);
            startActivity(intent);
            finish();
        }

    }
    private void requestAllPerms() {
        KLog.e("申请权限");
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)== PackageManager.PERMISSION_GRANTED
                &&ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED
                &&ContextCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE)== PackageManager.PERMISSION_GRANTED){
            KLog.e("申请权限成功");
            countTime();
            return;
        }
        RxPermissions rxPermissions=new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (!aBoolean){
                            ToastUtil.showShort(SplashActivity.this,"拒绝该权限将无法使用软件");

                            finish();
                        }else {
                            countTime();
                        }
                    }
                });

    }
}
